package com.hxkj.controller.taglib;

import com.hxkj.domain.po.FreeMarker;
import com.hxkj.client.service.taglib.AnalyzeArticleService;
import com.integral.mall.file.po.SysFile;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

/**
 * @ClassName AnalyzeArticleController
 * @Description
 * @Author chenfangzhou1
 * @Date 2018/7/20 17:04
 * @Version 1.0
 */
@RestController
@RequestMapping("/analyzeArticle")
public class AnalyzeArticleController {

    @Autowired
    private AnalyzeArticleService analyzeArticleService;

    /**
     * 解析自定义标签文章详情
     * @param freeMarker
     * @return
     */
    @RequestMapping(value = "/article")
    public Callable<ResultDataStruct> analyzeArticle(@RequestBody FreeMarker freeMarker) {
        return() -> {
            ResultDataStruct resultDataStruct = new ResultDataStruct();
            try {
                analyzeArticleService.analyzeArticle(freeMarker);
                resultDataStruct.setCode(200);
            } catch (Exception e) {
                e.printStackTrace();
                resultDataStruct.setCode(400);
                resultDataStruct.setMessage("失败");
            }
            return resultDataStruct;
        };
    }


    /**
     * 文章可视化
     * @param freeMarker
     * @return
     */
    @RequestMapping(value = "/visualizationArticle")
    public ResultDataStruct<SysFile> visualizationArticle(@RequestBody FreeMarker freeMarker) throws Exception {
        ResultDataStruct<SysFile> resultDataStruct = new ResultDataStruct<>();
        try {
            SysFile sysFile = analyzeArticleService.visualizationArticle(freeMarker);
            resultDataStruct.setData(sysFile);
            resultDataStruct.setCode(200);
            resultDataStruct.setMessage("成功");
        }catch (CustomException e){
            resultDataStruct.setCode(e.getCode().getCode());
            resultDataStruct.setMessage(e.getMessage());
        }catch (CustomMessageException e){
            resultDataStruct.setCode(e.getCode());
            resultDataStruct.setMessage(e.getMessage());
        }
        return resultDataStruct;
    }
}
